package com.android.androidbigwork.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.android.androidbigwork.R;
import com.android.androidbigwork.wedget.CircleImageView;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class RegistryActivity extends AppCompatActivity {
    private String id;
    private String name;
    private String sex;
    private String passWord;
    private File headerPhoto;
    private static final String getIdUrl="http://39.108.183.17:8080/get_registry_id";
    private static final String registryUrl="http://39.108.183.17:8080/registry";

    private EditText Account_editText;
    private EditText name_editText;
    private EditText sex_editText;
    private EditText passWord_editText;
    private CircleImageView circleImageView;
    private Button registry;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registry);
        verifyStoragePermissions(this);

        Account_editText = findViewById(R.id.registryAccount);



        new Thread(new Runnable() {
            @Override
            public void run() {

                    RequestParams requestParams = new RequestParams(getIdUrl);
                    x.http().get(requestParams, new Callback.CacheCallback<String>() {
                        @Override
                        public boolean onCache(String result) {
                            return false;
                        }

                        @Override
                        public void onSuccess(String result) {
                            id = result;
                            Account_editText.setText(id);
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {

                        }

                        @Override
                        public void onCancelled(CancelledException cex) {

                        }

                        @Override
                        public void onFinished() {

                        }
                    });

            }
        }).start();
        circleImageView = findViewById(R.id.headerPhoto);
        name_editText = findViewById(R.id.userName);
        sex_editText = findViewById(R.id.sex);
        passWord_editText = findViewById(R.id.registry_password);
        registry = findViewById(R.id.registry);




        circleImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, 1);
            }
        });

        registry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = name_editText.getText().toString();
                sex = sex_editText.getText().toString();
                passWord = passWord_editText.getText().toString();

                RequestParams params = new RequestParams(registryUrl);
                params.addBodyParameter("id",id);
                params.addBodyParameter("name",name);
                params.addBodyParameter("headerPhoto",headerPhoto);
                params.addBodyParameter("sex",sex);
                params.addBodyParameter("password",passWord);
                Log.d("", "名字"+name);
                x.http().post(params, new Callback.CacheCallback<String>() {
                    @Override
                    public boolean onCache(String result) {
                        return false;
                    }

                    @Override
                    public void onSuccess(String result) {
                        Toast.makeText(RegistryActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(RegistryActivity.this,LoginActivity.class);
                        startActivity(intent);

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            Uri uri=data.getData();
            String[] files={MediaStore.Images.Media.DATA};
            Cursor c=this.getContentResolver().query(uri, files, null, null, null);
            c.moveToFirst();
            int ii=c.getColumnIndex(files[0]);
            String path =c.getString(ii);
            c.close();
            Log.d("", "点击相册："+path);

            headerPhoto = new File(path);

            try {
                FileInputStream fis = new FileInputStream(path);
                Bitmap header = BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片
                circleImageView.setImageBitmap(header);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }



    private  final int REQUEST_EXTERNAL_STORAGE = 1;
    private  String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE };
    public  void verifyStoragePermissions(AppCompatActivity activity) {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
    }
}
